package com.sl.biz.modular.process.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.biz.modular.process.controller.request.SquareLakeProcessQueryByConditionRequest;
import com.sl.biz.modular.process.controller.request.SquareLakeProcessRemoveByIdRequest;
import com.sl.biz.modular.process.controller.response.ProcessSimpleVO;
import com.sl.biz.modular.process.controller.response.ProcessVO;
import com.sl.biz.modular.process.controller.response.ProcessVersionVO;
import com.sl.biz.modular.process.dao.domain.dto.ProcessConfigDTO;
import com.sl.biz.modular.process.dao.domain.dto.ProcessVersionDTO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessConfigDO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessVersionDO;
import com.sl.biz.modular.process.dao.service.ProcessConfigDaoService;
import com.sl.biz.modular.process.service.ProcessConfigBizService;
import com.sl.biz.modular.process.service.ProcessVersionBizService;
import com.sl.core.engine.ProcessEngine;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ProcessConfigBizServiceImpl implements ProcessConfigBizService {

    private final ProcessConfigDaoService processConfigDaoService;

    private final ProcessVersionBizService processVersionBizService;

    @Override
    public String save(ProcessConfigDO addRequest) {
        String id = addRequest.getId();
        if (StrUtil.isNotBlank(id)) {
            ProcessVersionDO processVersionDO = new ProcessVersionDTO();
            processVersionDO.setProcessId(id);
            List<ProcessVersionVO> list = processVersionBizService.list(processVersionDO);
            if (CollUtil.isNotEmpty(list)) {
                for (ProcessVersionVO processVersionVO : list) {
                    processVersionBizService.clearCache(processVersionVO);
                }
            }
            processConfigDaoService.updateById(addRequest);

        } else {
            processConfigDaoService.save(addRequest);
        }
        ProcessEngine.INSTANCE.clearPool();
        return addRequest.getId();
    }

    @Override
    public ProcessVO getById(ProcessConfigDO addRequest) {
        return BeanUtil.copyProperties(processConfigDaoService.getById(addRequest.getId()), ProcessVO.class);

    }

    @Override
    public List<ProcessSimpleVO> list(SquareLakeProcessQueryByConditionRequest queryByConditionRequest) {

        if (StrUtil.equals(queryByConditionRequest.getGroupId(), "0")) {
            queryByConditionRequest.setGroupId(null);
        }
        List<ProcessConfigDTO> processConfigDTOS = processConfigDaoService.queryByGroupIdAndProductId(queryByConditionRequest.getGroupId(),
                queryByConditionRequest.getProductId(), queryByConditionRequest.getKeywords());
        return BeanUtil.copyToList(processConfigDTOS, ProcessSimpleVO.class);
    }

    @Override
    public Boolean removeById(SquareLakeProcessRemoveByIdRequest queryByConditionRequest) {
//        Boolean b = processRepositoryManager.process().removeById(queryByConditionRequest.getId());
        //还需要删除配置表
        boolean b = processConfigDaoService.removeById(queryByConditionRequest.getId());
        ProcessVersionDO processVersionDO = new ProcessVersionDTO();
        processVersionDO.setProcessId(queryByConditionRequest.getId());
        List<ProcessVersionVO> list = processVersionBizService.list(processVersionDO);
        if (CollUtil.isNotEmpty(list)) {
            for (ProcessVersionVO processVersionVO : list) {
                processVersionBizService.removeById(BeanUtil.copyProperties(processVersionVO, ProcessVersionDO.class));
            }
        }
        //
        return b;
    }

    @Override
    public List<ProcessVO> getByIds(List<String> processIds) {
        return BeanUtil.copyToList(processConfigDaoService.getByIds(processIds), ProcessVO.class);
    }
}
